/*
 * $Log: $
 */
package com.rock.code.datacollconfig.coll.service;



import com.rock.code.common.util.SpringContext;
import com.rock.code.common.util.SpringContextUtil;
import com.rock.code.datacollconfig.coll.dao.IDataCollDao;
import com.rock.code.datacollconfig.coll.dao.IDataCollModeDao;
import com.rock.code.datacollconfig.coll.domain.DataColl;
import com.rock.code.datacollconfig.coll.domain.DataCollMode;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Title: DataCollInstanceJob<br>
 * Description: <br>
 * 数据采集
 * @author Huosd
 * @createDate May 25, 2012
 */
@Component
public class DataCollInstanceJob implements Job {
//	@Resource
	private IDataCollDao dataCollDao;
//	@Resource
	private IDataCollModeDao dataCollModeDao;

//	private IDataCollService iDataCollService;
	
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		// 获取采集id
		String id = String.valueOf(dataMap.get("ID"));
		// 采集dao
//		dataCollDao = (IDataCollDao) SpringContext.getBeanByClass("dataCollDao");
//		iDataCollService = (IDataCollService) SpringContextUtil.getBeanByClass(IDataCollService.class);
		dataCollDao = (IDataCollDao) SpringContextUtil.getBeanByClass(IDataCollDao.class);
		// 查询采集信息
		DataColl dataColl = dataCollDao.getDataCollById(id);
		try {
			this.collData(dataColl);
		} catch (Exception exception) {
			exception.printStackTrace();
		}

	}

	/**
	 * 采集数据
	 * @param dataColl
	 */
	@SuppressWarnings("unchecked")
	public void collData(DataColl dataColl) throws Exception {
		// 获取采集方式
		String collMode = dataColl.getCollModeId();
		if (DataCollMode.DATA_COLL_MODE_IF.equals(collMode)) { // 如果是接口采集
			Class ss = Class.forName(dataColl.getCollInterface());
			// 参数1:采集ID 参数2：采集有效时间开始时间 参数3：采集有效时间结束时间
			String[] parameters = new String[] { dataColl.getId(),
					dataColl.getCollTimeStartEffectiveDate(),
					dataColl.getCollTimeEndEffectiveDate(),dataColl.getCollParam() };
			Object arglist[] = new Object[1];
			Class partypes[] = new Class[1];
			partypes[0] = String[].class;
			arglist[0] = parameters;
			Method meth = ss.getMethod("execute", partypes);
			meth.invoke(ss.newInstance(), arglist);
		}
	}
}
